package com.hdwx.weike.controllers;

import com.alibaba.fastjson.JSON;
import com.hdwx.weike.beans.PageBean;
import com.hdwx.weike.beans.VideoSection;
import com.hdwx.weike.beans.VideoType;
import com.hdwx.weike.service.IHotSearchService;
import com.hdwx.weike.service.IVideoService;
import com.hdwx.weike.service.impl.HotSearchServiceImpl;
import com.hdwx.weike.service.impl.VideoServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * Created by hdwx on 2016/12/26.
 */
@WebServlet(urlPatterns = "/videolist")
public class VideoController extends BaseController {
    IVideoService videoService = new VideoServiceImpl();
    IHotSearchService hotSearchService=new HotSearchServiceImpl();

    @Override
    public void todo(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取search参数，不为空则进行模糊搜索
        String search = req.getParameter("search");
        System.out.println("videolist模糊搜索："+search);
        //获取前端传来的页码
        String index = req.getParameter("pageIndex");
        int pageIndex = 1;//默认第一页
        if (index != null) {
            try {
                pageIndex = Integer.valueOf(index);
            } catch (Exception e) {}
        }
        //获取分段id
        String sid = req.getParameter("sectionId");
        int sectionId = 0;//默认全部分段
        if (sid != null) {
            try {
                sectionId = Integer.valueOf(sid);
            } catch (Exception e) {}
        }
        //获取分类id
        String tid = req.getParameter("typeId");
        int typeId = 0;//默认全部分类
        if (tid != null) {
            try {
                typeId = Integer.valueOf(tid);
            } catch (Exception e) {}
        }
        //获取排序id
        String pid = req.getParameter("sortId");
        int sortId = 0;//默认最新排序
        if (pid != null) {
            try {
                sortId = Integer.valueOf(pid);
            } catch (Exception e) {}
        }
        //查询所有的分段
        List<VideoSection> sections = videoService.queryAllVideoSection();
        //查询所有的分类
        List<VideoType> types = videoService.queryAllVideoType();
        req.setAttribute("sections", sections);
        req.setAttribute("types", types);
        PageBean pageBean=new PageBean();
        if (search!=null) {
            String name = req.getParameter("keyName");
            if(name!=null&&name!="") {
                //处理搜索的关键字
                hotSearchService.dealSearchKey(name);
                //通过关键字搜索视频
                System.out.println("模糊搜索："+name);
                pageBean = videoService.queryByName(name, pageIndex, sortId);
                req.setAttribute("pageBean", pageBean);
                req.setAttribute("keyName",name);
                req.getRequestDispatcher("videolist.jsp").forward(req, resp);
            }
        } else {

//            System.out.println(pageIndex + "::" + sectionId + "::" + typeId);
            pageBean = videoService.queryByIndex(pageIndex, sectionId, typeId, sortId);
            req.setAttribute("pageBean", pageBean);
            req.getRequestDispatcher("videolist.jsp").forward(req, resp);

        }

    }
}
